<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Country_model extends CI_Model
{
	var $id  ;

        var $iso2;

        var $iso3;

        var $numcode;
	
	var $vi_name  ;
	
	var $en_name  ;

        var $active;

        var $position;

        function __construct() {
            parent::__construct();
        }

	function insert($data){

		$sql = "INSERT INTO `gtb_country` " .
				"(" .
				"`iso2`, `iso3`, `numcode`, `vi_name`, `en_name`, `active`, `position`)" .
				" VALUES ";

		$sql .= "(" .
			$this->db->escape($data['iso2']).", " .
                        $this->db->escape($data['iso3']).", " .
                        $this->db->escape($data['numcode']).", " .
                        $this->db->escape($data['vi_name']).", " .
                        $this->db->escape($data['en_name']).", " .
                        intval($data['active']).", " .
                        intval($data['position']).
		")";

		$this->db->query($sql);
		return $this->db->insert_id();

	}

	function update($data){
		 $sql = "UPDATE gtb_country
			SET ".
                         " iso2 = " .  $this->db->escape($data['iso2']) . "," .
                         " iso3 = " .  $this->db->escape($data['iso3']) . "," .
                         " numcode = " .  $this->db->escape($data['numcode']) . "," .
			 " vi_name = " .  $this->db->escape($data['vi_name']) . "," .
                         " en_name = " .  $this->db->escape($data['en_name']) . "," .
                         " active = " .  $this->db->escape($data['active']) . "," .
			 " position = " .  intval($data['position']) .
				" WHERE id = " .intval($data['id']);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

	function delete($id){
		// Kiem tra trc khi xoa, neu co record tham chieu thi ko cho xoa.
		// .......... chua lam

		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_country WHERE id IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

	function getAll(){
		$result = null;
		$sql = "SELECT * " .
				" FROM gtb_country " .
				" ORDER BY id DESC";

		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}

	function getAllCount(){
		$result = null;
		$sql = "SELECT COUNT(*) as num_record " .
				" FROM gtb_country " ;

		$query = $this->db->query($sql);
		$result = $query->result();

		if($result) return $result[0]->num_record;

		return 0;
	}

	
	function getMaxPosition(){

		$sql = "SELECT MAX(position) as position_max
				FROM gtb_country
                        ";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}

		return 0;
	}

	function getById($id){

		$sql = "SELECT *
				FROM gtb_country
				WHERE id = " . intval($id);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}

		return null;
	}
/*
	 * Get province by country
	 * */
	function getProvince($country_id){
		$result = null;
		$sql = "SELECT * " .
				" FROM gtb_province " .
				" WHERE id_country =".$country_id."".
				" ORDER BY position DESC";

		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}
}

// EOF

